package org.wendu.wdoa.api;


import org.wendu.wdoa.common.Department;
import org.wendu.wdoa.common.UpdataUser;
import org.wendu.wdoa.common.page.Page;
import org.wendu.wdoa.dto.InfoDto;
import org.wendu.wdoa.model.EmpUser;
import org.wendu.wdoa.common.Result;
import org.wendu.wdoa.service.InfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Validated
@RestController
@RequestMapping("/info/emp")
public class InfoController {

    @Resource
    private InfoService infoService;


    @GetMapping("")
    public Result<Page<EmpUser>> roleList(InfoDto dto){
        Page<EmpUser> page = infoService.getRolePage(dto);
        return Result.OK(page);

    }

    @GetMapping("/add-deps")
    public Result allDepList(){
        List<Department> depList = infoService.getAllDep();
        return Result.OK(depList);
    }

//新增员工
    @PostMapping("/add")
    public Result addNew(@RequestBody EmpUser empUser){
        infoService.addNewUser(empUser);
        return Result.OK("新增用户成功");
    }


    //删除员工
    @DeleteMapping("/delete/{id}")
    public Result deleteOld(@PathVariable Integer id){
        infoService.deleteOldUser(id);
        return Result.OK("删除用户成功");
    }

    //查询e_id是否存在
    @GetMapping("/find/{id}")
    public Result findUser(@PathVariable Integer id){
        int userId = infoService.findUserId(id);
        if (userId == 0){
            return Result.OK();
        }else{
            return Result.err(userId,"该编号已经存在");
        }
    }
    //修改
    @PutMapping("/modify")
    public Result updateUser(@RequestBody UpdataUser user){
        infoService.updataUser(user.getD_id(),user.getE_id());
        return Result.OK("修改用户成功");
    }





}
